﻿package pk;

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class FileReplaceKeywords {

    public static void main(String[] args) throws Exception {

        if (args.length != 4) {
            System.out
                    .println("Usage:java FileReplaceKeywords sourceFile targetFile initWord newWord ");
            System.exit(0);
        }
        String sourceFile = args[0];
        String targetFile = args[1];
        String initWord = args[2];
        String newWord = args[3];
        File source = new File(sourceFile);
        File target = new File(targetFile);

        if (!source.exists()) {
            System.out.println("-- Sorry,source file：" + sourceFile
                    + " can't be found. --");
            System.exit(0);
        }

        Scanner input = new Scanner(source);
        PrintWriter writer = new PrintWriter(target);
        while (input.hasNext()) {
            String newStr = input.nextLine().replaceAll(initWord, newWord);
            writer.write(newStr + "\n");
        }
        input.close();
        writer.close();
        System.out.println("--- finish ----");

    }

}

